/*
** grh_action2.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:37:44 2011 michael guidet
** Last update Mon Jul  4 15:40:26 2011 michael guidet
*/

#include "server.h"

void	grh_ppo(t_fd *fd, t_env *e, char *cmd)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  int	temppos;
  int	id;
  int	r;

  r = x_int(-1, sscanf(cmd, "ppo %d", &id), "sscanf");
  if (r == 1)
    for (tmp = e->fd; tmp; tmp = tmp->next)
      if (tmp->type == AI)
	if (tmp->pl->id == id)
	  break;
  if (r != 1 || tmp == NULL)
    {
      add_string_to_list(&fd->send_list, "sbp\n");
      return ;
    }
  temppos = (tmp->pl->look_at == NORD) ? 1 :
    (tmp->pl->look_at == EST) ? 2 :
    (tmp->pl->look_at == SUD) ? 3 : 4;
  sprintf(cmd2, "ppo %d %d %d %d\n", id, tmp->pl->x, tmp->pl->y,
	  temppos);
  add_string_to_list(&fd->send_list, cmd2);
}

void	grh_plv(t_fd *fd, t_env *e, char *cmd)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  int	id;
  int	r;

  r = x_int(-1, sscanf(cmd, "plv %d", &id), "sscanf");
  if (r == 1)
    for (tmp = e->fd; tmp; tmp = tmp->next)
      if (tmp->type == AI)
	if (tmp->pl->id == id)
	  break;
  if (r != 1 || tmp == NULL)
    {
      add_string_to_list(&fd->send_list, "sbp\n");
      return ;
    }
  sprintf(cmd2, "plv %d %d\n", id, tmp->pl->level);
  add_string_to_list(&fd->send_list, cmd2);
}

void	grh_pin(t_fd *fd, t_env *e, char *cmd)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  int	id;
  int	r;

  r = x_int(-1, sscanf(cmd, "pin %d", &id), "sscanf");
  if (r == 1)
    for (tmp = e->fd; tmp; tmp = tmp->next)
      if (tmp->type == AI)
	if (tmp->pl->id == id)
	  break;
  if (r != 1 || tmp == NULL)
    {
      add_string_to_list(&fd->send_list, "sbp\n");
      return ;
    }
  sprintf(cmd2, "pin %d %d %d %d %d %d %d %d %d %d\n", id, tmp->pl->x,
	  tmp->pl->y, tmp->pl->ressource[NOURRITURE],
	  tmp->pl->ressource[LINEMATE], tmp->pl->ressource[DERAUMERE],
	  tmp->pl->ressource[SIBUR], tmp->pl->ressource[MENDIANE],
	  tmp->pl->ressource[PHIRAS], tmp->pl->ressource[THYSTAME]);
  add_string_to_list(&fd->send_list, cmd2);
}

void	grh_sgt(t_fd *fd, t_env *e, char *cmd)
{
  char	cmd2[BUF_SIZE];

  sprintf(cmd2, "sgt %d\n", e->t);
  add_string_to_list(&fd->send_list, cmd2);
  cmd = cmd;
  e = e;
}

void	grh_sst(t_fd *fd, t_env *e, char *cmd)
{
  t_fd	*tmp;
  char	cmd2[BUF_SIZE];
  int	new_t;
  int	r;

  r = x_int(-1, sscanf(cmd, "sst %d", &new_t), "sscanf");
  if (r != 1 || new_t <= 0 || new_t > 500)
    {
      add_string_to_list(&fd->send_list, "sbp\n");
      return ;
    }
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == AI)
      {
	tmp->pl->life = (long)((float)tmp->pl->life *
				  ((float)e->t / (float)new_t));
	if (tmp->idaction != -1)
	  tmp->wait = (long)((float)tmp->wait * ((float)e->t / (float)new_t));
      }
  e->t = new_t;
  sprintf(cmd2, "sgt %d\n", e->t);
  for (tmp = e->fd; tmp; tmp = tmp->next)
    if (tmp->type == GRAPH)
      add_string_to_list(&tmp->send_list, cmd2);
}
